package com.xiang.handler;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
@ChannelHandler.Sharable
public class ClientIdleHandler extends IdleStateHandler {
    public ClientIdleHandler( ) {
        super(0, 3, 0, TimeUnit.SECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        if(evt == IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT){
            log.info(" client first write idle state event 触发");
            // 3s 未写数据则发送 心跳到服务端
            ctx.channel().writeAndFlush("hearbeat dump dump....");
        }
        super.channelIdle(ctx, evt);
    }
}
